package com.wugui.datax.admin.controller;

import com.ext.ToTxt;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * @author dxy
 * @date 2020/12/15 14:04
 */

@RestController
@RequestMapping("/ext/export")
public class ExtExportController {

    @RequestMapping("/txt")
    public String export(
            @NotBlank String dbType,
            @NotBlank String host,
            @NotNull Integer port,
            @NotBlank String schema,
            @NotBlank String username,
            @NotBlank String password,
            @NotBlank String table,
            String filePath,
            String ftpHost, Integer ftpPort, String ftpUseName, String ftpPassword, String ftpPath,
            String dateField, String dateRange) {
        ToTxt toTxt = new ToTxt(dbType);
        if(StringUtils.isNotBlank(filePath)) {
            toTxt.setFilePath(filePath);
        }
        if(StringUtils.isNotBlank(ftpHost)) {
            toTxt.setFtpHost(ftpHost);
        }
        if(ftpPort != null) {
            toTxt.setFtpPort(ftpPort);
        }
        if(StringUtils.isNotBlank(ftpUseName)) {
            toTxt.setFtpUseName(ftpUseName);
        }
        if(StringUtils.isNotBlank(ftpHost)) {
            toTxt.setFtpHost(ftpHost);
        }
        if(StringUtils.isNotBlank(ftpPassword)) {
            toTxt.setFtpPassword(ftpPassword);
        }
        if(StringUtils.isNotBlank(ftpPath)) {
            toTxt.setFtpPath(ftpPath);
        }
        try {
            toTxt.apply(host, port, schema, username, password, table, dateField, dateRange);
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
        return "OK";
    }
}
